﻿
using System;
using Npgsql;
using Forums.Domain;
using System.IO;
using System.Data;

namespace Forums.Data
{
	public delegate Object  DoWithcommand(Object o, IDbCommand c);
	
	public class GenericDaoImpl
	{
		
		public Object Execute(Object o,DoWithcommand d)
		{
			IDbConnection con = null;
			Object retVal = null;
			
			try
			{
				//Step 1 : connection open
				con = ConnectionUtils.OpenIDbConnection();
				
				//step2 : create cmd
				IDbCommand dbcmd = con.CreateCommand();
				
				//step 3 - do something
				retVal =  d(o,dbcmd);
				
			}
			finally
			{
				//step4 :Connection close
				ConnectionUtils.CloseIDbConnection(con);
				
			}
			
			return retVal;
		}
		
		

		public static IDbDataParameter AddParameter(IDbCommand dbcmd, string paramName, DbType type, object value)
		{
			IDbDataParameter parameter = dbcmd .CreateParameter();
			parameter.ParameterName = paramName;
			parameter.DbType = type;
			if (value != null)
				parameter.Value = value;
			else
				parameter.Value = DBNull.Value;
			dbcmd.Parameters.Add(parameter);
			return parameter;
		}
		
		
	}
}

